#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <gnome-keyring.h>

#include "account_manage.h"
#include "global_vars.h"

static GString *get_settings_directory_path()
{
	GString *settings_directory_path;

	settings_directory_path=g_string_new(g_get_home_dir());

	settings_directory_path=g_string_append(settings_directory_path,"/.njubras-for-linux");

	return settings_directory_path;
}

static GString *get_username_file_path()
{
	GString *username_file_path;

	username_file_path=get_settings_directory_path();

	username_file_path=g_string_append(username_file_path,"/account");

	return username_file_path;
}

gboolean is_account_saved()
{
	GString *username_file;

	username_file=get_username_file_path();

	if (g_access(username_file->str,R_OK)==0)
		return TRUE;
	else
		return FALSE;
}
void save_account()
{
	GString *settings_directory_path;
	GString *username;
	GString *password;

	GnomeKeyringResult result;

	GString *username_file;
	GIOChannel *username_write_channel;

	GtkWidget *username_entry;
	GtkWidget *password_entry;

	settings_directory_path=get_settings_directory_path();

	username_entry=g_object_get_data(G_OBJECT(window_global),"username_entry");
	password_entry=g_object_get_data(G_OBJECT(window_global),"password_entry");

	username=g_string_new(gtk_entry_get_text(GTK_ENTRY(username_entry)));
	password=g_string_new(gtk_entry_get_text(GTK_ENTRY(password_entry)));

	if (g_access(settings_directory_path->str,F_OK)!=0)
		mkdir(settings_directory_path->str,0777);

	result=gnome_keyring_store_password_sync(GNOME_KEYRING_NETWORK_PASSWORD,
																	   		  NULL,"user's password",password->str,
														           			  "user",username->str,"server","njubras-for-linux",NULL);
	if (result!=GNOME_KEYRING_RESULT_OK)
		g_warning(_("Can't store the password. %s"),gnome_keyring_result_to_message(result));

	username_file=get_username_file_path();
	username_write_channel=g_io_channel_new_file(username_file->str,"w+",NULL);
	g_io_channel_write_chars(username_write_channel,username->str,-1,NULL,NULL);

	g_io_channel_close(username_write_channel);

	g_string_free(settings_directory_path,TRUE);
	g_string_free(username,TRUE);
	g_string_free(password,TRUE);
	g_string_free(username_file,TRUE);
}

void read_account()
{
	GtkWidget *username_entry;
	GtkWidget *password_entry;

	GString *username_file;
	GIOChannel *username_read_channel;

	GString *username;
	gsize term_pos;
	gchar *password_chars;

	GnomeKeyringResult result;

	username=g_string_new("");

	username_entry=g_object_get_data(G_OBJECT(window_global),"username_entry");
	password_entry=g_object_get_data(G_OBJECT(window_global),"password_entry");

	username_file=get_username_file_path();

	username_read_channel=g_io_channel_new_file(username_file->str,"r",NULL);
	g_io_channel_read_line_string(username_read_channel,username,&term_pos,NULL);
	username->str[term_pos]='\0';

	result=gnome_keyring_find_password_sync(GNOME_KEYRING_NETWORK_PASSWORD,
																		   &(password_chars),"user",username->str,"server",
																			"njubras-for-linux",NULL);
	if (result!=GNOME_KEYRING_RESULT_OK)
		g_warning(_("Can't find the password. %s"),gnome_keyring_result_to_message(result));

	gtk_entry_set_text(GTK_ENTRY(username_entry),username->str);

	if (password_chars!=NULL)
		gtk_entry_set_text(GTK_ENTRY(password_entry),password_chars);

	g_io_channel_close(username_read_channel);
	gnome_keyring_free_password(password_chars);
	g_string_free(username,TRUE);
	g_string_free(username_file,TRUE);
}

void remove_account()
{
	GString *username;

	GString *username_file;
	GIOChannel *username_read_channel;

	GnomeKeyringResult result;

	username=g_string_new("");
	username_file=get_username_file_path();

	username_read_channel=g_io_channel_new_file(username_file->str,"r",NULL);

	if (username_read_channel==NULL)
		return;

	g_io_channel_read_line_string(username_read_channel,username,NULL,NULL);

	result=gnome_keyring_delete_password_sync(GNOME_KEYRING_NETWORK_PASSWORD,
														             			"user",username->str,"server","njubras-for-linux",NULL);
	if (result!=GNOME_KEYRING_RESULT_OK)
		g_warning(_("Can't find the password. %s"),gnome_keyring_result_to_message(result));

	g_io_channel_close(username_read_channel);
	g_remove(username_file->str);

	g_string_free(username,TRUE);
	g_string_free(username_file,TRUE);
}

